<?php
if(!defined('BLOG'))exit('不作死就不会死');
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/1/18
 * Time: 13:53
 */
base::load_class('session_mem','',1);
base::load_class('cookie');
class index{
    private $db;
    public function __construct(){
        $this->db = base::load_model('users_model');
    }
    public function register(){
        $forminfo = my_addslashes($_POST);
        $new_username=$forminfo['username'];
        $passwd=$forminfo['passwd'];
        $captcha=$forminfo['captcha'];
        if ($captcha != strtolower($_SESSION['captcha'])) {
            echo json_encode(array(L('captcha_error')));exit;
        }
        if (!preg_match('/^[a-zA-Z\-_\d\.]+@[a-z\d]+\.[a-z\.]{2,6}/', $new_username)) {
            echo json_encode(array(L('email_error')));exit;
        }
        $where = ' where mail = "'.$new_username.'"';
        $username =  $this->db->get_one(array('mail'),$where);
        if ($username['mail']==$new_username) {
            echo json_encode(array(L('mail_regied')));exit();
        }
        $secret = createsecret();
        $data = array(
            'mail' => $new_username,
            'passwd' => hash("haval160,4",strmcrypt($passwd,$secret)),
            'secretkey' => $secret,
        );
        $this->db->insert($data);
        $_SESSION['userlogin']=true;
        cookie::set_cookie("username",$new_username);
        cookie::set_cookie("login",true,'','','','',$mcrpty=false);
       echo json_encode(array('login' =>true));
    }
}